package home.control.service;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.util.Log;

public class ServerAdapter {
	// konfiguracja serwera
	private String host = "lewy.com.pl";
	private static final int SERVER_PORT = 2223;

	// wejściowy i wyjściowy strumień obiektów
	private ObjectOutputStream oos;
	private ObjectInputStream ois;

	private Socket clientSocket;

	private boolean connectionStatus;

	private static final String TAG = "ServerAdapter";

	public ServerAdapter(String host) {
		this.host = host;
		oos = null;
		ois = null;
		clientSocket = null;
		connectionStatus = false;
	}

	public void connectToServer(){

		try {
			clientSocket = new Socket(host, SERVER_PORT);

			Log.v(TAG, "Otwieranie Object Input Stream...");
			ois = new ObjectInputStream(clientSocket.getInputStream());

			Log.v(TAG, "Otwieranie Object Output Stream...");
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.flush();
			connectionStatus = true;
		} catch (UnknownHostException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}

		Log.v(TAG, "Połączono z serwerem");
	}

	public ObjectOutputStream getObjectOutputStream() {
		return oos;
	}

	public ObjectInputStream getObjectInputStream() {
		return ois;
	}

	public boolean isConnected() {
		return connectionStatus;
	}
}
